//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.tool.RuntimeException;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.Statements;

public class JsqlParserGlobal {
    private static JsqlParserFunction<String, Statement> parserSingleFunc = CCJSqlParserUtil::parse;
    private static JsqlParserFunction<String, Statements> parserMultiFunc = CCJSqlParserUtil::parseStatements;
    private static JsqlParseCache jsqlParseCache;

    public JsqlParserGlobal() {
    }

    public static Statement parse(String sql) throws JSQLParserException {
        if (jsqlParseCache == null) {
            return (Statement) parserSingleFunc.apply(sql);
        } else {
            Statement statement = jsqlParseCache.getStatement(sql);
            if (statement == null) {
                statement = (Statement) parserSingleFunc.apply(sql);
                jsqlParseCache.putStatement(sql, statement);
            }

            return statement;
        }
    }

    public static Statements parseStatements(String sql) throws JSQLParserException {
        if (jsqlParseCache == null) {
            return (Statements) parserMultiFunc.apply(sql);
        } else {
            Statements statements = jsqlParseCache.getStatements(sql);
            if (statements == null) {
                statements = (Statements) parserMultiFunc.apply(sql);
                jsqlParseCache.putStatements(sql, statements);
            }

            return statements;
        }
    }

    public static void setParserSingleFunc(final JsqlParserFunction<String, Statement> parserSingleFunc) {
        JsqlParserGlobal.parserSingleFunc = parserSingleFunc;
    }

    public static void setParserMultiFunc(final JsqlParserFunction<String, Statements> parserMultiFunc) {
        JsqlParserGlobal.parserMultiFunc = parserMultiFunc;
    }

    public static void setJsqlParseCache(final JsqlParseCache jsqlParseCache) {
        JsqlParserGlobal.jsqlParseCache = jsqlParseCache;
    }
}
